package com.zhaopin.springboot.web;

import com.zhaopin.springboot.model.Student;
import com.zhaopin.springboot.service.StudentService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Administrator
 * @create 2021-04-20 10:15
 */
@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping(value = "springboot/id")
    public Object getStudentById(Integer id) {

        return studentService.getStudentById(id);
    }

    @RequestMapping(value = "update")
    public Integer updateByKey(Integer id, String name, Integer age) {
        Student student = new Student();
        student.setId(id);
        student.setName(name);
        student.setAge(age);
        return studentService.updateByKey(student);
    }

    /**
     * restful 可以只提供一个接口 分别使用 put post get delete请求，就可以实现 crud四种操作
     * <p>
     * GET 请求查询数据
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "restful/{id}", method = RequestMethod.GET)
    public Object getStudentByIdRest(@PathVariable("id") Integer id) {
        return studentService.getStudentById(id);
    }

    /**
     * POST请求修改数据
     *
     * @param id
     * @param name
     * @param age
     * @return
     */
    @RequestMapping(value = "restful/{id}/{name}/{age}", method = RequestMethod.POST)
    public Object getStudentByIdRest1(@PathVariable("id") Integer id,
                                      @PathVariable("name") String name,
                                      @PathVariable("age") Integer age) {
        Student student = new Student();
        student.setId(id);
        student.setName(name);
        student.setAge(age);
        return studentService.updateByKey(student);

    }

    /**
     * GET 请求修改数据
     *
     * @param id
     * @param name
     * @param age
     * @return
     */
    @RequestMapping(value = "restful/{id}/{name}/{age}", method = RequestMethod.GET)
    public Object getStudentByIdRest2(@PathVariable("id") Integer id,
                                      @PathVariable("name") String name,
                                      @PathVariable("age") Integer age) {
        return studentService.getStudentById(id);
    }


    @RequestMapping(value = "/springboot/allStudentCount")
    public Object allStudentCount(){
        return studentService.allStudentCount();
    }
}
